$(function(){
    let ids;
    $.ajax({
        url: "http://chst.vip:1234/api/getbaicaijiatitle",
        type: "get",
        data: {},
        success(res) {
            let { result } = res;
            let html="";
            console.log(res)
            result.forEach((item, index, array) => {
                html+=`
                <li titleId=${item.titleId}>${item.title}</li>
                `
            })
            $(".nav>ul").html(html)
            drivepage(0)
            $(".nav li").click(function(){
                drivepage($(this).attr("titleId"))
                clearInterval(ids);
            })
        },
        error(err) {
            console.log(err);
        }
    })
    
    function drivepage(titleid){
        $.ajax({
            url: "http://chst.vip:1234/api/getbaicaijiaproduct",
            type: "get",
            data: {titleid:titleid},
            success(res) {
                let { result } = res;
                let html="";
                console.log(result)
                result.forEach((item, index, array) => {
                    html+=`
                    <li>
                        <a>
                            <div class="left">
                                ${item.productImg}
                            </div>
                            <div class="right">
                                <p class="goods">${item.productName}</p>
                                <p class="price">${item.productPrice}&nbsp;&nbsp;&nbsp;<del>1888.0</del></p>
                                <div class="residual">
                                    <div class="percentage"><div class="progress"><span class="barpercentage">0</span>%</div></div>
                                    <p>已领<span class="receive">0</span>张/剩余<span class="surplus">126</span>张</p>
                                </div>
                                <section>点此领取优惠券</section>
                                <section>下单链接</section>
                            </div>
                        </a>
                    </li>
                    `
                })
                $(".content>ul").html(html)
                let n=$(".surplus:eq(1)").text()
                let m;
                ids=setInterval(function(){ 
                    if(n==0){
                        clearInterval(ids);
                    }
                    m=Math.round((126-n)/126*100)
                    $(".receive").text(126-n);
                    $(".surplus").text(n--);
                    $(".percentage>div").css({width:m+"%"})
                    $(".barpercentage").text(m)
                },50);
            },
            error(err) {
                console.log(err);
            }
        })
    }
})